package cn.cyl.basic.controller;

import cn.cyl.basic.util.AjaxResult;
import cn.cyl.basic.util.FastDfsUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {


    //文件上传
    @PostMapping
    public AjaxResult uploadFile(@RequestPart MultipartFile file){
        try {
            //获取原始的文件名
            String oName = file.getOriginalFilename();//xx.jpg
            //截取获取类型
            String exeName = oName.substring(oName.lastIndexOf(".") + 1);//jpg

            //调工具类，需要提供文件的完全限定名 和 类型
            String fileName = FastDfsUtils.upload(file.getBytes(), exeName);

            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.setObj(fileName);
            return ajaxResult;
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(111);
            return new AjaxResult(false,"上传失败");
        }

    }

    //文件删除
    @DeleteMapping
    //从路径中获取参数                完整路径 /goup1/xxxxx/yyyy
    public AjaxResult deleteFile (@RequestParam(required = true,value = "path")String path){
        try {
            System.err.println(path+"===================================");
            path= path.substring(1);//    goup1/xxxxx/yyyy
            String groupName = path.substring(0, path.indexOf("/"));//  goup1
            String fileName = path.substring(path.indexOf("/")+1);//  xxxxx/yyyy
            //                 goup1       xxxxx/yyyy
            FastDfsUtils.delete(groupName,fileName);

           return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }


}
